import { playerDtMgr } from '../../../Framework/Data/DataBaseMgr';
import DataMgr from '../../../Framework/Data/DataMgr';
import ResMgr from '../../../Framework/Res/ResMgr';
import VecUtils from '../../Utils/VecUtils';
import Player from '../Object/Player';
import ModuleBase from './ModuleBase';
import ModuleMgr from './ModuleMgr';
import TiledMapCtrl from './TiledMapCtrl';
const { ccclass, property } = cc._decorator;

@ccclass
export default class PlayerLayer extends ModuleBase {
    //判断是否有玩家死亡
    private hasPlayerDie: boolean = false;
    //存储玩家的脚本
    private _arrPlayer: Player[] = [];
    onInit(...rest: any[]): void {
        this._onKeyKeyboardEvent();
    }

    private _onKeyKeyboardEvent() {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }

    private _removeKeyboardEvent() {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }

    onKeyDown(event: cc.Event.EventKeyboard) {
        for (let player of this._arrPlayer) {
            if (player.isDeath) {
                this.hasPlayerDie = true;
                return;
            }
        }
        for (let player of this._arrPlayer) {
            player.onKeyDown(event.keyCode);
        }
    }
    onKeyUp(event: cc.Event.EventKeyboard) {
        if (this.hasPlayerDie) {
            for (let player of this._arrPlayer) {
                if (!player.isDeath) {
                    player.statsCtrl.changeState("Idle");
                }
            }
            return;
        }
        for (let player of this._arrPlayer) {
            player.onKeyUp(event.keyCode);
        }
    }

    async createPlayer() {
        let arrRoleIndex: number[] = DataMgr.instance.getMgr<playerDtMgr>("playerDtMgr").arrPlayerIndex;
        let arrPos: cc.Vec2[] = ModuleMgr.instance.getModule<TiledMapCtrl>("TiledMapCtrl").getAllBirthPos();
        for (let i = 0; i < 2; i++) {
            // 获取预制体
            let playerPre = await ResMgr.instance.getRes<cc.Prefab>("Game", "Role" + (arrRoleIndex[i] + 1));
            let playerNode = cc.instantiate(playerPre);
            //将玩家的脚本存储数组中，方便后续对玩家进行移动
            let playerTs = playerNode.getComponent(Player);
            //设置当前是第几个玩家
            playerTs.playerIndex = i + 1;
            //当前玩家选择的是哪个角色
            playerTs.roleIndex = arrRoleIndex[i];
            playerTs.playerLayer = this;
            //调用player脚本中的oninit函数
            playerTs.oninit();
            playerNode.parent = ModuleMgr.instance.getModule<TiledMapCtrl>("TiledMapCtrl").node;
            this._arrPlayer.push(playerTs);
            let index = Math.floor(Math.random() * arrPos.length);
            let pos: cc.Vec2 = arrPos[index];
            playerNode.setPosition(VecUtils.getVec3(pos));
            // 将这个地址数组中的用过的地址删除
            arrPos.splice(index, 1);
        }
    }
    onUpdate(dt: number, ...rest: any[]): void {
        for (let player of this._arrPlayer) {
            player.onUpdate.apply(player, arguments);
        }
    }

    getPlayerTsByTiledPos(tiledPos: cc.Vec2) {
        for (let player of this._arrPlayer) {
            if (tiledPos.equals(player.playerTiledPos)) {
                return player;
            }
        }
        return null;
    }
}